#include "minitouch.h"
#include "adbutils.h"
#include <QtCore/qdir.h>

MiniTouch::MiniTouch(QObject *parent) : QObject(parent)
{

}

QMutex MiniTouch::mutex;
QAtomicPointer<MiniTouch> MiniTouch::instance = 0;

MiniTouch* MiniTouch::getInstance()
{
#ifndef Q_ATOMIC_POINTER_TEST_AND_SET_IS_ALWAYS_NATIVE
    if(!QAtomicPointer<MiniTouch>::isTestAndSetNative())
        qDebug("Error: TestAndSetNative not supported!");
#endif

    if(instance.testAndSetOrdered(0, 0))
    {
        QMutexLocker locker(&mutex);
        instance.testAndSetOrdered(0, new MiniTouch);
    }
    return instance;
}

int MiniTouch::prepare() {
    int device = AdbUtils::getDevicesNum();
    if (device > 1) return -1;
    if (device <= 0) return -2;
    QString abi = AdbUtils::getProp(ABI_PROP);
    if (abi.length() <= 0) return -3;

    AdbUtils::push(QDir::currentPath().append(MINI_TOUCH_BINARY_DIR).append(abi).append("/").append(MINI_TOUCH_BINARY_NAME), MINI_EXECUTE_DEVICE_DIR);
    AdbUtils::chmod("777", QString(MINI_EXECUTE_DEVICE_DIR).append(MINI_TOUCH_BINARY_NAME));
    return 0;
}
